package boot

import (
	"gorm.io/gorm/logger"
	"time"
)

// OssConfig 系统配置信息
type OssConfig struct {
	Database DataBaseConfig `yaml:"database"`
	Server   ServerConfig   `yaml:"server"`
	Storage  StorageConfig  `yaml:"storage"`
	User     UserConfig     `yaml:"user"`
}

// UserConfig 账户配置
type UserConfig struct {
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Email    string `yaml:"email"`
}

// StorageConfig 存储配置
type StorageConfig struct {
	Default  string          `yaml:"default"`
	Cloud    StorageCloud    `yaml:"cloud"`
	Location StorageLocation `yaml:"location"`
}

// StorageCloud 云存储配置
type StorageCloud struct {
	Endpoint        string `yaml:"endpoint"`
	AccessKeyId     string `yaml:"access-key-id"`
	AccessKeySecret string `yaml:"access-key-secret"`
	UseCname        bool   `yaml:"use-cname"`
	BucketName      string `yaml:"bucket-name"`
}

// StorageLocation 本地存储
type StorageLocation struct {
	Path string `yaml:"path"`
}

// DataBaseConfig 数据库相关配置
type DataBaseConfig struct {
	Base   DBBase   `yaml:"base"`
	Logger DBLogger `yaml:"logger"`
}

type ServerConfig struct {
	Port           string        `yaml:"port"`
	ContextPath    string        `yaml:"context-path"`
	ReadTimeout    time.Duration `yaml:"read-timeout"`
	WriteTimeout   time.Duration `yaml:"write-timeout"`
	MaxHeaderBytes int           `yaml:"max_header_bytes"`
}

func DefaultServerConfig() ServerConfig {
	return ServerConfig{
		Port:           ":8081",
		ContextPath:    "/oss-cloud",
		ReadTimeout:    20,
		WriteTimeout:   20,
		MaxHeaderBytes: 1 << 25,
	}
}

// DBBase 基础信息
type DBBase struct {
	Link            string `yaml:"link"`               // 连接
	Prefix          string `yaml:"prefix"`             // 前缀
	Singular        bool   `yaml:"singular"`           // 单表表名
	AutoPing        bool   `yaml:"auto-ping"`          // ping
	MaxIdleConns    int    `yaml:"max-idle-conns"`     // 空闲连接池中连接的最大数量
	MaxOpenConns    int    `yaml:"max-open-conns"`     // 打开数据库连接的最大数量
	ConnMaxLifeTime int    `yaml:"conn_max_life_time"` // 连接可复用的最大时间
}

// DBLogger 数据路日志相关配置
type DBLogger struct {
	Level string `yaml:"level"` // 日志等级
	Slow  uint   `yaml:"slow"`  // 慢SQL阀值
	Color bool   `yaml:"color"` // 禁用彩色打印
	Path  string `yaml:"path"`  // 日志路径
}

func (l *DBLogger) GetLevel() logger.LogLevel {
	switch l.Level {
	case "debug":
		return logger.Info
	case "warn":
		return logger.Warn
	case "error":
		return logger.Error
	default:
		return logger.Silent
	}
}
